<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <!-- 搜索区域 -->
      <a-form layout="inline">
        <a-row :gutter="10">
          <a-col :md="10" :sm="12">
            <a-form-item label="账号" style="margin-left:8px">
              <a-input placeholder="请输入账号" v-model="queryParam.username"></a-input>
            </a-form-item>
          </a-col>
          <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
            <a-col :md="6" :sm="24">
              <a-button type="primary" @click="searchQuery" icon="search" style="margin-left: 18px">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </a-col>
          </span>
        </a-row>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator" :md="24" :sm="24" style="margin-top: -15px;margin-left:8px">
      <a-button @click="handleAddUserProject" type="primary" icon="plus">添加已有用户</a-button>
      <a-button @click="handleAdd" type="primary" icon="plus" style="margin-top: 16px">新建用户</a-button>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="doBatchDel">
            <a-icon type="delete"/>
            取消关联
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;margin-left:8px;margin-right:8px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">
          {{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        style="margin-left:8px;margin-right:8px"
        rowKey="id"
        :columns="columns"
        :scroll="{x:true}"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <!--<a @click="handleEdit(record)">编辑</a>-->
          <a href="javascript:;" @click="handleDetail(record)">用户详情</a>
          <a-divider type="vertical"/>
          <a-popconfirm title="确定取消与选中角色关联吗?" @confirm="() => doDelete(record.id)">
            <a>取消关联</a>
          </a-popconfirm>
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <app-role-user-modal ref="modalForm" @ok="modalFormOk"></app-role-user-modal>
    <!-- 添加已有用户 -->
    <Select-User-Modal ref="selectUserModal" @selectFinished="selectOK"></Select-User-Modal>

    <user-tenant-modal ref="userTenantModal" @ok="modalFormOk"></user-tenant-modal>
  </a-card>
</template>

<script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import AppRoleUserModal from './modules/AppRoleUserModal'
  import SelectUserModal from './modules/SelectUserModal'
  import UserTenantModal from './modules/UserTenantModal'
  import { api, addExistUser } from './service'
  import { TENANT_ID } from '@/store/mutation-types';
  import Vue from 'vue';
  export default {
    name: 'AppRoleUserInfo',
    mixins: [JeecgListMixin],
    components: {
      AppRoleUserModal,
      SelectUserModal,
      UserTenantModal
    },
    data() {
      return {
        description: '用户信息',
        disableMixinCreated: true,
        roleId: '',
        // 表头
        columns: [{
            title: '账号',
            dataIndex: 'username'
          },
          {
            title: '姓名',
            dataIndex: 'realname'
          },
          {
            title: '性别',
            align: 'center',
            dataIndex: 'sex_dictText'
          },
          {
            title: '电话',
            dataIndex: 'phone'
          },
          {
            title: '操作',
            dataIndex: 'action',
            scopedSlots: { customRender: 'action' },
            align: 'center',
            fixed: 'right',
            width: 150
          }],
        url: {
          list: '',
          delete: '',
          deleteBatch: api.deleteBatch(this.roleId)
        }
      }
    },
    created() {
      this.tenantId = Vue.ls.get(TENANT_ID)
      this.roleId = this.$route.params.roleId;
      this.url.list = api.list(this.roleId)
      this.searchQuery();
    },

    methods: {
      searchReset() {
        this.queryParam = {}
        this.url.list = api.list(this.roleId)
        this.loadData(1);
      },
      doBatchDel: function () {
        this.url.deleteBatch = api.deleteBatch(this.roleId);
        this.batchDel();
      },
      doDelete(id) {
        this.url.delete = api.delete(id, this.roleId);
        this.handleDelete(id);
      },
      hasSelectDept() {
        if (this.currentDeptId === '') {
          this.$message.error('请选择一个部门!')
          return false;
        }
        return true;
      },
      handleDetail: function (record) {
        this.$refs.userTenantModal.edit(record, this.tenantId);
        this.$refs.userTenantModal.title = '详情';
        this.$refs.userTenantModal.disableSubmit = true;
      },
      handleAdd: function () {
        this.$refs.modalForm.add(this.tenantId);
        this.$refs.modalForm.title = '新增';
        this.$refs.modalForm.disableSubmit = false;
        this.$refs.modalForm.roleId = this.roleId;
      },
      handleAddUserProject() {
        if (this.currentDeptId === '') {
          this.$message.error('请选择一个部门!')
        } else {
          this.$refs.selectUserModal.add();
        }
      },
      selectOK(data) {
        let params = {};
        params.roleId = this.roleId;
        params.userIdList = [];
        for (var a = 0; a < data.length; a++) {
          params.userIdList.push(data[a]);
        }
        console.log(params);
        addExistUser(this.roleId, params).then((res) => {
          if (res.success) {
            this.$message.success(res.message);
            this.loadData();
          } else {
            this.$message.warning(res.message);
          }
        })
      }
    }
  }
</script>
<style scoped>
  /** Button按钮间距 */
  .ant-btn {
    margin-left: 3px
  }

  .ant-card {
    margin-left: -30px;
    margin-right: -30px;
  }

  .table-page-search-wrapper {
    margin-top: -16px;
    margin-bottom: 16px;
  }
</style>
